//
//  SkuSelectView.m
//  Yueshi
//
//  Created by Northwan on 15/9/8.
//  Copyright (c) 2015年 悦食中国 All rights reserved.
//

#import "SkuSelectView.h"
#import "YSSubProduct.h"
#import "YSProdSkuView.h"

#define CHILD_HEIGHT 35
#define MARGIN 10

@interface SkuSelectView()
@property (strong, nonatomic) UIButton *selectBtn;
@property (strong, nonatomic) NSArray *prodArray;
@property (strong, nonatomic) NSMutableArray *btnArray;
@end

@implementation SkuSelectView

-(instancetype)initWithProdSubArray:(NSArray *)array{
    self = [super init];
    if (self) {
        self.width = SCREENW;
//        NSArray *resultArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
//            return ((YSSubProduct *)obj1).prodBuyLink.length <((YSSubProduct *)obj2).prodBuyLink.length;
//        }];
        
        _prodArray = array;
        _btnArray = [NSMutableArray array];
        
        for (int i =0; i<_prodArray.count; i++) {
            YSSubProduct *sub = _prodArray[i];
            
            UIButton *btn = [[UIButton alloc]init];
            [btn.titleLabel setFont:SYS_FONT(15)];
            [btn setTag:i];
            [btn setCornerRadius:5 borderWidth:0.5 borderColor:HEXColor(@"#b9b9b9")];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [btn setTitle:sub.prodBuyLink forState:UIControlStateNormal];
            
            [btn setTitleColor:HEXColor(@"#333333") forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
            //这个地方给btn设置背景的时候,我发现用imageWithColor的时候会造成btn本身不能自适应文字长度，以后在遇到要注意这点
            [btn setBackgroundImage:[UIImage resizedImage:@"btn_bg_gray_normal"] forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage resizedImage:@"bg_btn_gray"] forState:UIControlStateDisabled];
            [btn setBackgroundImage:[UIImage resizedImage:@"bg_btn_red"] forState:UIControlStateSelected];
            [btn setBackgroundImage:[UIImage resizedImage:@"bg_btn_red"] forState:UIControlStateHighlighted];
            
            [self addSubview:btn];
            
            [self.btnArray addObject:btn];
            
            if(_prodArray.count==1){//如果只有一个的时候就把ta设置默认选中
                [btn setSelected:YES];
                _selectBtn = btn;
            }
            
            if (sub.isCanBuy == 0) {//不可购买，状态为disable
                [btn setEnabled:NO];
                if([_delegate respondsToSelector:@selector(skuBtnClickWithId:isCanbuy:)]){
                    [_delegate skuBtnClickWithId:btn.tag isCanbuy:NO];
                }
            }
        }
        
        //更新ui，对btn进行排列
        [self updateLayout];
    }
    return self;
}

- (void)updateLayout{
    CGFloat parentW = self.width;
    CGFloat currentX = YSPublicXInset;
    CGFloat currentY = YSPublicXInset;
    for (int i = 0; i < _btnArray.count; i++) {
        UIButton *btn = _btnArray[i];
        [btn sizeToFit];
        btn.width = btn.width + YSPublicXInset;
        btn.height = CHILD_HEIGHT;
        if (btn.width+currentX > parentW-MARGIN*2) {
            btn.x = YSPublicXInset;
            btn.y = currentY + CHILD_HEIGHT * 1.5;
            if (btn.width > parentW-MARGIN*2) {
                btn.width = parentW-MARGIN*2;
            }
        }else{
            btn.x = currentX;
            btn.y = currentY;
        }
        currentY = btn.y;
        currentX = CGRectGetMaxX(btn.frame) + MARGIN;
        NSLog(@"btn frame----%@",NSStringFromCGSize(btn.size));
    }
    self.height = CGRectGetMaxY(((UIView *)[[self subviews] lastObject]).frame)+YSPublicXInset;
}

-(BOOL)hasOneChecked{
    return _selectBtn!=nil;
}

-(void)btnClick:(UIButton *)btn{
    if(_selectBtn!=nil && _selectBtn.tag == btn.tag){
        return;
    }else{
        if([_prodArray[btn.tag] isCanBuy] == 0){
            [btn setEnabled:NO];
            [btn setBackgroundImage:[UIImage imageWithColor:RGBColor(204, 204, 204)] forState:UIControlStateDisabled];
            if([_delegate respondsToSelector:@selector(skuBtnClickWithId:isCanbuy:)]){
               [_delegate skuBtnClickWithId:btn.tag isCanbuy:NO];
            }
        }else{
            if([_delegate respondsToSelector:@selector(skuBtnClickWithId:isCanbuy:)]){
                [_delegate skuBtnClickWithId:btn.tag isCanbuy:YES];
            }
            [btn setSelected:YES];
            [_selectBtn setSelected:NO];
            _selectBtn = btn;
        }
    }
}

-(NSUInteger)getSelectIndex{
    return _selectBtn.tag;
}

-(YSSubProduct *)getSelectProdSub{
    return _prodArray[_selectBtn.tag];
}

-(void)clearSelected{
    if (_selectBtn) {
        [_selectBtn setSelected:NO];
        _selectBtn = nil;
    }
}

@end
